<template>
  <el-container v-loading="loading">

    <el-row>
      <el-button
        icon="fa fa-fw fa-refresh"
        size="mini"
        type="info"
        @click="handleRefresh">
      </el-button>

      <el-button
        icon="fa fa-fw fa-plus"
        size="mini"
        type="primary"
        @click="handleAddItem">添加
      </el-button>

      <el-button
        icon="fa fa-fw fa-trash-o"
        size="mini"
        type="danger"
        @click="handleDelete">删除
      </el-button>

      <el-button
        icon="fa fa-fw fa-toggle-on"
        size="mini"
        type="success"
        @click="handleStartUsing">启用
      </el-button>

      <el-button
        icon="fa fa-fw fa-toggle-off"
        size="mini"
        type="warning"
        @click="handleBlockUp">停用
      </el-button>
    </el-row>

    <el-table
      @selection-change="handleSelectionChange"
      :data="tableData"
      tooltip-effect="dark"
      border
      :default-sort="{prop: 'date', order: 'descending'}"
      :row-class-name="tableRowClassName"
      size="small">
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>

      <el-table-column
        prop="name"
        label="标题">
      </el-table-column>

      <el-table-column
        prop="min_price"
        label="最低消费"
        width="150"
        show-overflow-tooltip>
        <template slot-scope="scope">
          <i class="fa fa-fw fa-rmb"></i>
          <span style="margin-left: 10px">{{ scope.row.min_price }}</span>
        </template>
      </el-table-column>

      <el-table-column
        prop="price"
        label="抵扣金额"
        width="150"
        show-overflow-tooltip>
        <template slot-scope="scope">
          <i class="fa fa-fw fa-rmb"></i>
          <span style="margin-left: 10px">{{ scope.row.price }}</span>
        </template>
      </el-table-column>

      <el-table-column
        prop="status"
        label="状态"
        width="150"
        show-overflow-tooltip>
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == 1" type="success">正常</el-tag>
          <el-tag v-if="scope.row.status == 0" type="danger">停止</el-tag>
        </template>
      </el-table-column>

      <el-table-column
        prop="start_time"
        label="开始时间"
        sortable>
        <template slot-scope="scope">
          <i class="fa fa-clock-o fa-fw"></i>
          <span style="margin-left: 10px">{{ scope.row.start_time }}</span>
        </template>
      </el-table-column>

      <el-table-column
        prop="end_time"
        label="结束时间"
        sortable>
        <template slot-scope="scope">
          <i class="fa fa-clock-o fa-fw"></i>
          <span style="margin-left: 10px">{{ scope.row.end_time }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            circle
            icon="fa fa-fw fa-pencil"
            size="mini"
            type="primary"
            title="编辑"
            @click="handleEditItem(scope.$index, scope.row)">
          </el-button>

          <el-button
            circle
            icon="fa fa-fw fa-trash-o"
            size="mini"
            type="danger"
            title="删除"
            @click="handleDeleteItem(scope.$index, scope.row)">
          </el-button>

          <el-button
            circle
            icon="fa fa-fw fa-toggle-on"
            size="mini"
            type="success"
            title="启用"
            @click="handleStartUsingItem(scope.$index, scope.row)">
          </el-button>

          <el-button
            circle
            icon="fa fa-fw fa-toggle-off"
            size="mini"
            type="warning"
            title="停用"
            @click="handleBlockUpItem(scope.$index, scope.row)">
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pagination.currentPage"
      :page-sizes="pagination.pageSizes"
      :page-size="pagination.pageSize"
      :total="pagination.total"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>

  </el-container>
</template>

<script>
  export default {
    mounted: function () {
      this.getData();
    },
    methods: {
      /**
       * 数据请求
       * */
      getData: function () {
        let _this = this;
        this.api.getRewardTabulation({
          page: this.pagination.currentPage,
          paginate: this.pagination.pageSize
        }).then(function (res) {
          if (res.code == 1) {
            _this.tableData = res.data.data;
            _this.pagination.total = res.data.total;
          }
          _this.loading = false;
        }).catch(function (res) {
          throw new Error(res);
        });
      },
      /**
       * 删除数据
       * */
      delData: function ({id: ids}) {
        let _this = this;
        _this.api.delReward({id: ids}).then(function (res) {
          if (res.code == 1) {
            _this.BaseUtils.delByIds({idType: 'id', idValue: ids, dataArr: _this.tableData});
            _this.$message({
              showClose: true,
              message: res.msg,
              type: 'success'
            });
          } else {
            _this.$message({
              showClose: true,
              message: res.msg,
              type: 'warning'
            });
          }
        }).catch(function (error) {
          throw new Error(error);
        });
      },
      /**
       * 启用数据
       * */
      startUsingData: function ({id: ids}) {
        let _this = this;
        _this.api.updateRewardStatus({id: ids, status: 1}).then(function (res) {
          if (res.code == 1) {
            _this.BaseUtils.updateStatusByIds({status: 1, idValue: ids, dataArr: _this.tableData});
            _this.$message({
              showClose: true,
              message: res.msg,
              type: 'success'
            });
          } else {
            _this.$message({
              showClose: true,
              message: res.msg,
              type: 'warning'
            });
          }
        }).catch(function (error) {
          throw new Error(error);
        });
      },
      /**
       * 停用数据
       * */
      blockUpData: function ({id: ids}) {
        let _this = this;
        _this.api.updateRewardStatus({id: ids, status: 0}).then(function (res) {
          if (res.code == 1) {
            _this.BaseUtils.updateStatusByIds({status: 0, idValue: ids, dataArr: _this.tableData});
            _this.$message({
              showClose: true,
              message: res.msg,
              type: 'success'
            });
          } else {
            _this.$message({
              showClose: true,
              message: res.msg,
              type: 'warning'
            });
          }
        }).catch(function (error) {
          throw new Error(error);
        });
      },
      /**
       * 分页数改变
       */
      handleSizeChange: function (pageSize) {
        this.pagination.pageSize = pageSize;
        this.getData();
      },
      /**
       * 当前页面改变
       * @param currentPage
       */
      handleCurrentChange: function (currentPage) {
        this.pagination.currentPage = currentPage;
        this.getData();
      },
      tableRowClassName({row, rowIndex}) {
        if (rowIndex === 1) {
          return 'warning-row'
        } else if (rowIndex === 3) {
          return 'success-row'
        }
        return ''
      },
      /**
       * 编辑单个
       * */
      handleEditItem(index, row) {
        this.$router.push({
          name: 'FullSubtractionEditor',
          params: {
            id: row.id,
            methodType: this.constant.EDITORMETHOD
          }
        })
      },
      /**
       * 添加单个
       */
      handleAddItem: function () {
        this.$router.push({
          name: 'FullSubtractionEditor',
          params: {
            methodType: this.constant.ADDMETHOD
          }
        })
      },
      /**
       * 删除单个
       * @param index
       * @param row
       */
      handleDeleteItem(index, row) {
        let _this = this;
        let _id = row.id;
        this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          _this.delData({id: _id});
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消该操作'
          })
        })
      },
      /**
       * 启用单个
       * @param index
       * @param row
       */
      handleStartUsingItem(index, row) {
        let _this = this;
        let _id = row.id;
        this.$confirm('此操作将启用该数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          _this.startUsingData({id: _id});
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消该操作'
          })
        })
      },
      /**
       * 停用单个
       * @param index
       * @param row
       */
      handleBlockUpItem(index, row) {
        let _this = this;
        let _id = row.id;
        this.$confirm('此操作将停用该数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          _this.blockUpData({id: _id});
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消该操作'
          })
        })
      },
      /**
       * 数据刷新
       */
      handleRefresh: function () {
        this.getData();
        this.$message({
          showClose: true,
          message: '刷新成功',
          type: 'success'
        });
      },
      /**
       * 批量删除
       */
      handleDelete: function () {
        let _this = this;
        let arr = [];
        if (this.multipleSelection.length <= 0) {
          _this.$message({
            showClose: true,
            message: '暂无选中',
            type: 'warning'
          });
        }
        for (var i = 0; i < this.multipleSelection.length; i++) {
          arr.push(this.multipleSelection[i].id);
        }
        this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          _this.delData({id: arr.join(',')});
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消该操作'
          })
        })
      },
      /**
       * 批量停用
       */
      handleBlockUp: function () {
        let _this = this;
        let arr = [];
        if (this.multipleSelection.length <= 0) {
          _this.$message({
            showClose: true,
            message: '暂无选中',
            type: 'warning'
          });
        }
        for (var i = 0; i < this.multipleSelection.length; i++) {
          arr.push(this.multipleSelection[i].id);
        }
        this.$confirm('此操作将停用该数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          _this.blockUpData({id: arr.join(',')});
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消该操作'
          })
        })
      },
      /**
       * 批量启用
       */
      handleStartUsing: function () {
        let _this = this;
        let arr = [];
        if (this.multipleSelection.length <= 0) {
          _this.$message({
            showClose: true,
            message: '暂无选中',
            type: 'warning'
          });
        }
        for (var i = 0; i < this.multipleSelection.length; i++) {
          arr.push(this.multipleSelection[i].id);
        }
        this.$confirm('此操作将停用该数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          _this.startUsingData({id: arr.join(',')});
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消该操作'
          })
        })
      },
      /**
       * 表格选中状态记录
       * @param val
       */
      handleSelectionChange(val) {
        this.multipleSelection = val;
      }
    },
    data() {
      return {
        loading: true,  // 页面加载
        tableData: [
          {
            // id:4,                //类型：Number  必有字段  备注：键值
            // name:"789",                //类型：String  必有字段  备注：名称
            // min_price:"0.00",                //类型：String  必有字段  备注：最小金额
            // price:"50.00",                //类型：String  必有字段  备注：奖励金额
            // start_time:"2018-08-22 16:44:14",                //类型：String  必有字段  备注：开始时间
            // end_time:"2018-08-22 16:44:14",                //类型：String  必有字段  备注：结束时间
            // status:1                //类型：Number  必有字段  备注：1启用0关闭
          },
        ],
        multipleSelection: [],
        pagination: {
          total: 5,
          currentPage: 1,
          pageSize: 10,
          pageSizes: [5, 10, 20, 25, 30, 50, 100],
        },
      }
    },
  }
</script>

<style scoped>
  .el-container {
    display: block;
    width: 98%;
    margin: 0 auto;
  }

  .el-table {
    width: 100% !important;
  }

  .el-row, .el-pagination {
    margin: 15px 0;
  }

  .el-pagination {
    text-align: center;
  }
</style>
